﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UserDefineForm.exception;
using UserDefineForm.formcomponent;
using UserDefineForm.control.propertymapping;

namespace UserDefineForm.control
{
    class DesignableDatePicker:DesignableControlBase
    {

        override public String Value
        {
            set
            {
                DateTimePicker datePicker = (DateTimePicker)this.control;
                DateTime dateValue;
                try
                {
                    dateValue=DateTime.ParseExact(value, "dd/MM/yyyy", null);
                }
                catch
                {
                    throw new InvalidValueException(InvalidValueException.INVALID_DATE + value);
                }

                datePicker.Value = dateValue;
            }

            get
            {
                DateTimePicker datePicker = (DateTimePicker)this.control;
                return String.Format("{0:dd/MM/yyyy}", datePicker.Value); ;
            }
        }

        public DesignableDatePicker()
        {
        }

        public DesignableDatePicker(DesignPanel designPanel, PropertiesPanelv2 propPnl)
            : base(designPanel, propPnl)
        {
            this.control = new DateTimePicker();
            this.mappingObject = new DatePickerMappingObject(); 

            DateTimePicker datePicker = (DateTimePicker)control;

            datePicker.Format = DateTimePickerFormat.Custom;
            datePicker.CustomFormat = "dd/MM/yyyy";
            datePicker.Dock = DockStyle.Fill;

            this.mainPnl.Controls.Add(this.control);
            this.Size = new System.Drawing.Size(90, 20);
        }

        public override ControlType getType()
        {
            return ControlType.DatePicker;
        }
    }
}
